package com.ruiben.mall.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruiben.mall.IAdminService;
import com.ruiben.mall.IUserService;
import com.ruiben.mall.mapper.AdminMapper;
import com.ruiben.mall.mapper.UserMapper;
import com.ruiben.mall.pojo.Admin;
import com.ruiben.mall.pojo.User;
import com.ruiben.mall.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private Sid sid;


    @Override
    public int registerAdmin(Admin admin) {
        admin.setId(sid.nextShort());

        return adminMapper.insert(admin);
    }

    @Override
    public boolean queryUsernameIsExist(String username) {
        Admin admin = new Admin();
        admin.setUsername(username);

        Admin result = adminMapper.selectOne(admin);

        return result != null;
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Admin queryAdminForLogin(String username, String password) {

        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", password);

        return adminMapper.selectOneByExample(userExample);
    }

    public boolean deleteAdmin(String id){
        int i = adminMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    public boolean batchAdmins(String[] ids){
        Integer integer = adminMapper.batchDelAdmins(ids);
        return integer > 0;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        Example adminExample = new Example(Admin.class);
        Example.Criteria criteria = adminExample.createCriteria();
        criteria.andEqualTo("id", admin.getId());
        int i = adminMapper.updateByExampleSelective(admin, adminExample);
        return i > 0;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedResult getAdminList(int page,int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Admin> admins = adminMapper.selectAll();
        PageInfo<Admin> pageList = new PageInfo<>(admins);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(admins);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Admin queryUserInfoById(String userId) {
        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("id", userId);
        return adminMapper.selectOneByExample(userExample);
    }
}
